

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;

import testrank.affinity.MyStreamTokenizer;

public class LinesWithCodeFinder {
	public static void main(String[] args) throws IOException {
		String filePath = args[0];
		//Reader reader = readFile(filePath);
		Reader reader = new FileReader(filePath);
		MyStreamTokenizer tokenizer = new MyStreamTokenizer(reader);
		
	    // Prepare the tokenizer for Java-style tokenizing rules
		//tokenizer.parseNumbers();
		//tokenizer.wordChars('_', '_');
		//tokenizer.eolIsSignificant(true);

	    // If whitespace is not to be discarded, make this call
		//tokenizer.ordinaryChars(0, ' ');

		
		tokenizer.slashStarComments(true);
		tokenizer.slashSlashComments(true);
		tokenizer.eolIsSignificant(false);
	
		int ttype = 0;
		int lastline = -1;
		String s = "";
		while (ttype != StreamTokenizer.TT_EOF) {
			ttype = tokenizer.nextToken();
			
			int lineno = tokenizer.lineno();
			String sval = ttype == StreamTokenizer.TT_WORD ? tokenizer.sval : "";
			if (lineno == lastline) {
				s += " " + sval;
			}
			else {
				///if (lastline != -1)
					System.out.println(lastline + "\t" + s);
				s = sval;
			}
			lastline = lineno;
		}
	}
	
	  /**
    This constructor created a new <code>Category</code> instance and
    sets its name.

    <p>It is intended to be used by sub-classes only. You should not
    create categories directly.

    @param name The name of the category.
 */	
	static StringReader readFile(String fileName) {
		StringBuilder sb = new StringBuilder(); 
		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			BufferedReader input = new BufferedReader(new FileReader(fileName));
			try {
				String line = null; // not declared within while loop
				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					sb.append(line).append('\n');
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		//System.out.println(sb.toString());
		return new StringReader(sb.toString());
	}

	void alt() {
	try {
	    // Create the tokenizer to read from a file
	    FileReader rd = new FileReader("filename.java");
	    StreamTokenizer st = new StreamTokenizer(rd);

	    // Prepare the tokenizer for Java-style tokenizing rules
	    st.parseNumbers();
	    st.wordChars('_', '_');
	    st.eolIsSignificant(true);

	    // If whitespace is not to be discarded, make this call
	    st.ordinaryChars(0, ' ');

	    // These calls caused comments to be discarded
	    st.slashSlashComments(true);
	    st.slashStarComments(true);

	    // Parse the file
	    int token = st.nextToken();
	    while (token != StreamTokenizer.TT_EOF) {
	        token = st.nextToken();
	        switch (token) {
	        case StreamTokenizer.TT_NUMBER:
	            // A number was found; the value is in nval
	            @SuppressWarnings("unused")
	            double num = st.nval;
	            break;
	        case StreamTokenizer.TT_WORD:
	            // A word was found; the value is in sval
	            @SuppressWarnings("unused")
	            String word = st.sval;
	            break;
	        case '"':
	            // A double-quoted string was found; sval contains the contents
	            @SuppressWarnings("unused")
	            String dquoteVal = st.sval;
	            break;
	        case '\'':
	            // A single-quoted string was found; sval contains the contents
	            @SuppressWarnings("unused")
	            String squoteVal = st.sval;
	            break;
	        case StreamTokenizer.TT_EOL:
	            // End of line character found
	            break;
	        case StreamTokenizer.TT_EOF:
	            // End of file has been reached
	            break;
	        default:
	            // A regular character was found; the value is the token itself
	            @SuppressWarnings("unused")
				char ch = (char)st.ttype;
	            break;
	        }
	    }
	    rd.close();
	} catch (IOException e) {
	}
	}
}
